package com.zego.wawaji.ui.wawa.synth;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.third.image.ImageLoader;
import com.zego.wawaji.ui.bean.wawa.MyWawa;
import com.zego.wawaji.ui.bean.wawa.Wawa;
import com.zego.wawaji.wawaji_client.util.NoDoubleClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 需合成娃娃列表
 * Created by Su on 2018/2/28.
 */
public class SynthWawaAdapter extends RecyclerView.Adapter<SynthWawaAdapter.ViewHolder> {
    private Context context;
    private LayoutInflater mLayoutInflater;
    private OnWawaChooseListener listener;
    private List<MyWawa> list = new ArrayList<>();

    private boolean clickable = true;    // 因列表的更新，限制item的点击频率

    public SynthWawaAdapter(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
        this.context = context;
    }

    public void setList(List<MyWawa> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void setClickable(boolean clickable) {
        this.clickable = clickable;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mLayoutInflater.inflate(R.layout.synth_wawa_list, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final MyWawa info = list.get(position);
        final Wawa wawa = ModuleMgr.getWawaMgr().getWawa(info.getWwTypeId());
        ImageLoader.loadRoundTop(context, wawa.getImg(), holder.wawaImg);

        holder.synthProp.setText("概率+" + wawa.getPr() + "%");
        holder.wawaImg.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (!clickable) return;
                if (listener != null) {
                    clickable = false;
                    listener.onChooseClick(info, wawa, position);
                }
            }
        });
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView wawaImg;
        private TextView synthProp;

        public ViewHolder(final View itemView) {
            super(itemView);
            wawaImg = (ImageView) itemView.findViewById(R.id.wawa_img);
            synthProp = (TextView) itemView.findViewById(R.id.synth_prop);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public void setItemClickListener(OnWawaChooseListener listener) {
        this.listener = listener;
    }
}